package com.yby6.common.utils.upload;


import com.yby6.common.utils.uuid.UUID;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

/**
 * @program: yangbuyi-rbac
 * @ClassName: ImageBase64Util
 * @create: 2021-06-30 17:59
 * @author: Yang Shuai
 * @since： JDK1.8
 * @ImageBase64Util: base64$
 **/

public class ImageBase64Util {
	//base64字符串转化成图片
	public static File base64ToFile(String base64) throws Exception {
		if (base64.contains("data:image")) {
			base64 = base64.substring(base64.indexOf(",") + 1);
		}
		base64 = base64.toString().replace("\r\n", "");
		//创建文件目录
		String prefix = ".jpeg";
		File file = File.createTempFile(UUID.randomUUID().toString(), prefix);
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		try {
			BASE64Decoder decoder = new BASE64Decoder();
			byte[] bytes = decoder.decodeBuffer(base64);
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(bytes);
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return file;
	}

	//图片转化成base64字符串
	//将图片文件转化为字节数组字符串，并对其进行Base64编码处理
	public static String GetImageStr(File imgFile) {
		InputStream in = null;
		byte[] data = null;
		//读取图片字节数组
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//对字节数组Base64编码
		BASE64Encoder encoder = new BASE64Encoder();
		//返回Base64编码过的字节数组字符串
		return encoder.encode(data);
	}

}
